//
//  PPFollowUserCell.m
//  pickpic
//
//  Created by GaoYongyue on 16/9/12.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPFollowUserCell.h"
#import "PPUser.h"
#import "PPNetworkService.h"
#import "PPFollowActionView.h"
@interface PPFollowUserCell ()
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UIButton *followButton;
@property (weak, nonatomic) IBOutlet UILabel *nikcLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *centerNickLabel;
@property (weak, nonatomic) IBOutlet UIView *botomLine;

@end
@implementation PPFollowUserCell

- (void)awakeFromNib
{
    [super awakeFromNib];

    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.cornerRadius = 18.f;

    _followButton.layer.masksToBounds = YES;
    _followButton.layer.cornerRadius = 4.f;
    _followButton.layer.borderWidth = kLineWidth;

    [_botomLine addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
}

- (void)setUser:(PPUser *)user
{
    _user = user;

    if (!user.timestamp) {
        _nikcLabel.text = _user.nick;
        _centerNickLabel.text = _user.nick;
    }
    else
    {
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[user.timestamp longLongValue]/1000];
        NSString *dateString = [date timeAgoForTimeline];

        NSString *nick = [NSString stringWithFormat:@"%@   %@", _user.nick, dateString];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:nick];
        [attributedString addAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRGBHex:0x191919]}  range:NSMakeRange(0, [_user.nick length])];
        [attributedString addAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRGBHex:0xb2b2b2]}  range:NSMakeRange([_user.nick length], [nick length] - [_user.nick length])];
        [attributedString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]}  range:NSMakeRange(0, [_user.nick length])];
        [attributedString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]}  range:NSMakeRange([_user.nick length], [nick length] - [_user.nick length])];
        _nikcLabel.attributedText = attributedString;
        _centerNickLabel.attributedText = attributedString;
    }

    _descLabel.text = _user.signature;


    if ([_user.avatar length])
    {
        NSString *url = [_user.avatar hasPrefix:@"http:"] ? _user.avatar : [NSString stringWithFormat:@"%@%@", _user.avatarHost, _user.avatar];

        [url applyToImageView:_avatarView];
    }
    else
    {
        _avatarView.image = [UIImage imageNamed:@"icon-person-70"];
    }

    if ([_user.signature length])
    {
        _nikcLabel.hidden = NO;
        _descLabel.hidden = NO;
        _centerNickLabel.hidden = YES;
    }
    else
    {
        _nikcLabel.hidden = YES;
        _descLabel.hidden = YES;
        _centerNickLabel.hidden = NO;
    }

    _followButton.hidden = NO;
    if ([_user.relationship integerValue] == 0 || [_user.relationship integerValue] == 1)
    {
        //秒友  我关注的人   已关注 //朋友  互相关注的人
        [_followButton setTitle:@"已关注" forState:UIControlStateNormal];
        _followButton.backgroundColor = [UIColor colorWithRGBHex:0xffdd17];
        _followButton.layer.borderColor = [UIColor clearColor].CGColor;
    }
    else if ([_user.relationship integerValue] == 2)
    {
        //没关系 未关注
        [_followButton setTitle:@"关注" forState:UIControlStateNormal];
        _followButton.backgroundColor = [UIColor clearColor];
        _followButton.layer.borderColor = [UIColor colorWithRGBHex:0x323232].CGColor;
    }
    else
    {
        _followButton.hidden = YES;
    }

}

- (IBAction)followButtonDidPress:(id)sender
{
    if ([_user.relationship integerValue] == 0 || [_user.relationship integerValue] == 1)
    {
        //取消关注
        PPFollowActionView *actionView = [[NSBundle mainBundle] loadNibNamed:@"PPFollowActionView" owner:nil options:nil].firstObject;
        actionView.frame = CGRectMake(0, self.delegateViewController.navigationController.view.height - actionView.height, self.delegateViewController.navigationController.view.width, actionView.height);

        DGPopoverView *popoverView = [[DGPopoverView alloc] initWithContainerView:self.delegateViewController.navigationController.view contentView:actionView options:DGPopoverViewAnimationOptionUpward backgroundStyle:DGPopoverViewBackgroundStyleDimmed];

        __weak typeof(self) weakSelf = self;
        __weak typeof(popoverView) weakPopover = popoverView;
        [actionView.cancelButton setActionBlock:^(UIControl *control) {
            [weakPopover dismissAnimated:YES completion:nil];
        } forControlEvents:UIControlEventTouchUpInside];

        [actionView.unfollowButton setActionBlock:^(UIControl *control) {
            [weakPopover dismissAnimated:YES completion:^{
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:weakSelf.delegateViewController.view animated:YES];
                [PPNetworkService unFollow:weakSelf.user.userID callback:^(id data, NSError *error) {
                    [hud hide:YES];
                    if (data)
                    {
                        //取消关注成功
                        weakSelf.user.relationship = @(2);
                        [weakSelf.followButton setTitle:@"关注" forState:UIControlStateNormal];
                        weakSelf.followButton.backgroundColor = [UIColor clearColor];
                        weakSelf.followButton.layer.borderColor = [UIColor colorWithRGBHex:0x323232].CGColor;
                        if (weakSelf.delegateViewController && [weakSelf.delegateViewController respondsToSelector:@selector(userCell:user:relationShipDidChanged:)]) {
                            [weakSelf.delegateViewController userCell:weakSelf user:weakSelf.user relationShipDidChanged:weakSelf.user.relationship];
                        }
                        [weakSelf.delegateViewController.view toastMessage:@"操作成功" duration:1.f];
                        
                        [[NSNotificationCenter defaultCenter] postNotificationName:PPRelationshipDidChangeNotification object:weakSelf.user.userID];
                    }
                    else
                    {
                        [weakSelf.delegateViewController.view toastMessage:error.networkPrompt duration:1.f];
                    }
                }];

            }];
        } forControlEvents:UIControlEventTouchUpInside];

        [popoverView displayAnimated:YES completion:nil];
    }
    else if ([_user.relationship integerValue] == 2)
    {
        //关注
        __weak typeof(self)weakSelf = self;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:_delegateViewController.view animated:YES];
        [PPNetworkService follow:_user.userID callback:^(id data, NSError *error) {
            [hud hide:YES];
            if (data)
            {
                //关注成功
                weakSelf.user.relationship = @([data integerValue]);

                [weakSelf.followButton setTitle:@"已关注" forState:UIControlStateNormal];
                weakSelf.followButton.backgroundColor = [UIColor colorWithRGBHex:0xffdd17];
                weakSelf.followButton.layer.borderColor = [UIColor clearColor].CGColor;
                if (weakSelf.delegateViewController && [weakSelf.delegateViewController respondsToSelector:@selector(userCell:user:relationShipDidChanged:)]) {
                    [weakSelf.delegateViewController userCell:weakSelf user:weakSelf.user relationShipDidChanged:weakSelf.user.relationship];
                }
                [weakSelf.delegateViewController.view toastMessage:@"操作成功" duration:1.f];

                [[NSNotificationCenter defaultCenter] postNotificationName:PPRelationshipDidChangeNotification object:_user.userID];
            }
            else
            {
                //关注失败
                [weakSelf.delegateViewController.view toastMessage:error.networkPrompt duration:1.f];
            }
        }];
    }
}
@end
